package superhero.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

/**
 *
 * @author Marcus Jimson December 2, 2013 Superhero Registry Project
 */
public class MenuBar
{

    private JMenuBar menuBar;
    private JMenu fileMenu = new JMenu();
    private JMenu factionMenu = new JMenu();
    private JMenuItem exitItem = new JMenuItem();
    private JMenuItem avgItem, xmenItem, justItem, sourceItem, aboutItem, customItem;
    private JFrame sourceWindow = new JFrame();
    private JButton okay;
    private JPanel factionPanel;
    private JPanel temp = new JPanel();
    private JLabel factionHolder;
    private ImageIcon factionPic;
    private String description;
    private JTextArea describe = new JTextArea();
    private Font font = new Font("Verdana",
            Font.BOLD,
            12);
    private Border border = BorderFactory.createLineBorder(Color.BLACK);
    private Dimension dimension = new Dimension(50,
            70);
    private Dimension empty = new Dimension(25,
            75);

    /**
     * MenuBar builds the menu bar on the SuperheroDisplayPanel
     */
    public MenuBar()
    {
        buildMenuBar();
    }

    private void buildMenuBar()
    {
        menuBar = new JMenuBar();

        buildFileMenu();
        buildfactionMenu();

        getMenuBar().
                add(fileMenu);
        getMenuBar().
                add(factionMenu);

        // setJMenuBar(menuBar);
    }

    private void buildFileMenu()
    {

        exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(new ExitListener());

        sourceItem = new JMenuItem("Source");
        sourceItem.setMnemonic(KeyEvent.VK_S);
        sourceItem.addActionListener(new SourceListener());

        aboutItem = new JMenuItem("About");
        aboutItem.setMnemonic(KeyEvent.VK_A);
        aboutItem.addActionListener(new AboutListener());

        customItem = new JMenuItem("Create Hero");
        customItem.setMnemonic(KeyEvent.VK_C);
        customItem.addActionListener(new CustomListener());

        fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);

        fileMenu.add(sourceItem);
        fileMenu.add(aboutItem);
        fileMenu.add(customItem);
        fileMenu.add(exitItem);
    }

    private void buildfactionMenu()
    {

        factionMenu = new JMenu("Faction");
        factionMenu.setMnemonic(KeyEvent.VK_C);

        avgItem = new JMenuItem("Avengers");
        xmenItem = new JMenuItem("X-Men");
        justItem = new JMenuItem("Justice League");
        avgItem.addActionListener(new AvgListener());
        avgItem.setMnemonic(KeyEvent.VK_G);
        xmenItem.addActionListener(new XmenListener());
        xmenItem.setMnemonic(KeyEvent.VK_M);
        justItem.addActionListener(new JustListener());
        justItem.setMnemonic(KeyEvent.VK_J);

        factionMenu.add(avgItem);
        factionMenu.addSeparator();
        factionMenu.add(xmenItem);
        factionMenu.addSeparator();
        factionMenu.add(justItem);
    }

    /**
     * @return the menuBar
     */
    public JMenuBar getMenuBar()
    {
        return menuBar;
    }

    private class ExitListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            System.exit(0);
        }
    }

    private class AvgListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if(sourceWindow.isVisible())
            {
                sourceWindow.removeAll();
                sourceWindow.dispose();
            }            
            sourceWindow = new JFrame();
            factionPanel = new JPanel(new BorderLayout());
            String factName = "Images//AvengerOrigin.jpg";
            factionPic = new ImageIcon(factName);
            factionHolder = new JLabel(factionPic);
            Dimension sizing = new Dimension(250,
                    180);
            JPanel borders = new JPanel(new BorderLayout());
            JPanel temp1 = new JPanel();
            temp1.setPreferredSize(empty);

            description = "The Avengers are Earth's mightiest heroes,"
                    + " formed to fight the foes no single hero could withstand."
                    + "The Avengers are the most prestigious and powerful"
                    + " super-hero team in the world, an ever-shifting"
                    + " assemblage of super-beings, adventurers and crime"
                    + " fighters devoted to protecting the planet from menaces"
                    + " beyond the scope of conventional authorities. ";

            okay = new JButton("OK");
            okay.addActionListener(new okListener());
            JPanel button = new JPanel();
            button.add(okay);

            describe.setText(description);
            describe.setWrapStyleWord(true);
            describe.setLineWrap(true);
            describe.setFont(font);
            describe.setBorder(border);
            describe.setPreferredSize(sizing);
            describe.setEditable(false);
            borders.add(temp1,
                    BorderLayout.NORTH);
            borders.add(describe,
                    BorderLayout.CENTER);
            temp.add(borders);
            sourceWindow.setResizable(false);

            factionPanel.add(factionHolder,
                    BorderLayout.WEST);
            factionPanel.add(temp,
                    BorderLayout.CENTER);
            factionPanel.add(button,
                    BorderLayout.SOUTH);

            sourceWindow.add(factionPanel);
            sourceWindow.setVisible(true);
            sourceWindow.pack();
        }
    }

    private class XmenListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {        
            if(sourceWindow.isVisible())
            {
                sourceWindow.removeAll();
                sourceWindow.dispose();
            }       
            sourceWindow = new JFrame();
            factionPanel = new JPanel(new BorderLayout());
            String factName = "Images//XMenOrigin.jpg";
            factionPic = new ImageIcon(factName);
            factionHolder = new JLabel(factionPic);
            Dimension sizing = new Dimension(250,
                    180);
            JPanel borders = new JPanel(new BorderLayout());
            JPanel temp1 = new JPanel();
            temp1.setPreferredSize(empty);

            description = "The X-Men are a team of mutant superheroes"
                    + " in the Marvel Universe. Under a cloud of increasing"
                    + " anti-mutant sentiment, Professor Xavier created"
                    + " a haven at his Westchester mansion to train young"
                    + " mutants to use their powers for the benefit of humanity,"
                    + "and to prove mutants can be heroes.";

            okay = new JButton("OK");
            okay.addActionListener(new okListener());
            JPanel button = new JPanel();
            button.add(okay);

            describe.setText(description);
            describe.setWrapStyleWord(true);
            describe.setLineWrap(true);
            describe.setFont(font);
            describe.setPreferredSize(sizing);
            describe.setEditable(false);
            borders.add(temp1,
                    BorderLayout.NORTH);
            borders.add(describe,
                    BorderLayout.CENTER);
            temp.add(borders);

            factionPanel.add(factionHolder,
                    BorderLayout.WEST);
            factionPanel.add(temp,
                    BorderLayout.CENTER);
            factionPanel.add(button,
                    BorderLayout.SOUTH);

            sourceWindow.add(factionPanel);
            sourceWindow.setVisible(true);
            sourceWindow.pack();
        }
    }

    private class JustListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {            
            if(sourceWindow.isVisible())
            {
                sourceWindow.removeAll();
                sourceWindow.dispose();
            }            
            sourceWindow = new JFrame();
            factionPanel = new JPanel(new BorderLayout());
            String factName = "Images//JusticeOrigin.jpg";
            factionPic = new ImageIcon(factName);
            factionHolder = new JLabel(factionPic);
            Dimension sizing = new Dimension(250,
                    180);
            JPanel borders = new JPanel(new BorderLayout());
            JPanel temp1 = new JPanel();
            temp1.setPreferredSize(empty);

            description = "The Justice League is the DC Universe's"
                    + " most powerful and premier superhero team,"
                    + " a strike force comprised of the world's mightiest"
                    + " heroes. They act as stalwart protectors of sentient"
                    + " life; Earth's first line of defense against terrestrial"
                    + ", extra-terrestrial, inter-dimensional and supernatural"
                    + " threats.";

            okay = new JButton("OK");
            okay.addActionListener(new okListener());
            JPanel button = new JPanel();
            button.add(okay);

            describe.setText(description);
            describe.setWrapStyleWord(true);
            describe.setLineWrap(true);
            describe.setBorder(border);
            describe.setFont(font);
            describe.setPreferredSize(sizing);
            describe.setEditable(false);
            borders.add(temp1,
                    BorderLayout.NORTH);
            borders.add(describe,
                    BorderLayout.CENTER);
            temp.add(borders);

            factionPanel.add(factionHolder,
                    BorderLayout.WEST);
            factionPanel.add(temp,
                    BorderLayout.CENTER);
            factionPanel.add(button,
                    BorderLayout.SOUTH);

            sourceWindow.add(factionPanel);
            sourceWindow.setVisible(true);
            sourceWindow.pack();
        }
    }

    private class SourceListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            sourceWindow = new JFrame();
            JPanel biblio = new JPanel(new BorderLayout());
            sourceWindow.add(biblio);
            JTextArea document = new JTextArea();
            sourceWindow.setVisible(true);
            String words;
            File inFile = new File("Sources.txt");

            try
            {
                Scanner reader = new Scanner(inFile);
                while (reader.hasNextLine())
                {
                    words = reader.nextLine();
                    document.append(words + "\n");
                }
            }
            catch (FileNotFoundException ex)
            {
                System.err.print(ex.getMessage());
            }

            document.setFont(font);
            document.setBorder(border);
            document.setEditable(false);
            document.setWrapStyleWord(true);
            biblio.add(document,
                    BorderLayout.CENTER);
            JPanel temporary = new JPanel();
            okay = new JButton("Ok");
            okay.addActionListener(new okListener());
            temporary.add(okay);
            biblio.add(temporary,
                    BorderLayout.SOUTH);
            sourceWindow.pack();
        }
    }

    private class okListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            sourceWindow.dispose();
        }
    }

    private class AboutListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            sourceWindow = new JFrame();
            JPanel biblio = new JPanel(new BorderLayout());
            sourceWindow.add(biblio);

            String paragraph = "This project, by Marcus Jimson,"
                    + " gives information regarding some of comic books"
                    + " greatest heroes. The information is sorted"
                    + " and displayed for easy access and readability.";

            String fileName = "Images//marvelDC.jpg";
            ImageIcon pic = new ImageIcon(fileName);
            JLabel picture = new JLabel(pic);
            biblio.add(picture,
                    BorderLayout.NORTH);

            JTextArea document = new JTextArea();
            sourceWindow.setVisible(true);

            document.setText(paragraph);

            document.setEditable(false);
            document.setWrapStyleWord(true);
            document.setLineWrap(true);
            document.setFont(font);
            document.setBorder(border);
            document.setPreferredSize(dimension);

            biblio.add(document,
                    BorderLayout.CENTER);
            JPanel temporary = new JPanel();
            okay = new JButton("Ok");
            okay.addActionListener(new okListener());
            temporary.add(okay);
            biblio.add(temporary,
                    BorderLayout.SOUTH);
            sourceWindow.pack();
        }
    }

    private class CustomListener
            implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            try
            {
                CustomDisplayPanel hero = new CustomDisplayPanel();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
